package fr.noe.amicaleloisirs.concours.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import fr.noe.amicaleloisirs.concours.constants.Constants;
import fr.noe.amicaleloisirs.concours.utils.FileUtil;

/**
 * Servlet implementation class GetListeJoueurs
 */
public class GetListeJoueurs extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private Logger logger = Logger.getLogger(GetListeJoueurs.class);
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetListeJoueurs() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("get received");
		
		String xml = FileUtil.read(Constants.PLAYER_LIST_FILE_URL);
		
		if (xml == null) {
			response.sendError(HttpServletResponse.SC_NO_CONTENT, "Le fichier contenant la liste des joueurs n'a pas ete trouve ou est vide.");
		}
		else {
			response.setContentType("text/xml;charset=utf-8");
			PrintWriter out = response.getWriter();
			out.write(xml);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
